package face.pay.alipay.service.impl;

import com.alibaba.fastjson.JSON;
import com.face.pay.IPayConfigServer;
import face.pay.alipay.service.IAliPayService;
import face.pay.dao.EnterpriseInfoDao;
import face.pay.dao.StoreInfoDao;
import face.pay.dao.TerminalInfoDao;
import face.pay.entity.EnterpriseInfo;
import face.pay.entity.StoreInfo;
import face.pay.entity.TerminalInfo;
import face.pay.util.HttpClientUtil;
import face.pay.util.MathUtil;
import face.pay.util.ResultMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.HashMap;
import java.util.Map;


@Service
@Slf4j
public class AliPayServiceImpl implements IAliPayService {


    public IPayConfigServer iWxPayConfigServer;


    private EnterpriseInfoDao enterpriseInfoDao;


    private StoreInfoDao storeInfoDao;


    private TerminalInfoDao terminalInfoDao;


    @Value("${payApi.domain}")
    private String PayApidomain;


    public AliPayServiceImpl() {
    }

    @Autowired
    public AliPayServiceImpl(IPayConfigServer iWxPayConfigServer, EnterpriseInfoDao enterpriseInfoDao, StoreInfoDao storeInfoDao, TerminalInfoDao terminalInfoDao) {
        this.iWxPayConfigServer = iWxPayConfigServer;
        this.enterpriseInfoDao = enterpriseInfoDao;
        this.storeInfoDao = storeInfoDao;
        this.terminalInfoDao = terminalInfoDao;
    }

    /**
     * 支付宝网页支付
     *
     * @param deviceId
     * @param total
     * @return
     */
    @Override
    public ResultMessage aliPay(String deviceId, String cpnId, String total, String act_total) {


        Double money = null;
        try {
            money = Double.parseDouble(total);
        } catch (NumberFormatException e) {
            log.info("微信扫码支付金额有误!!!  {}", e.toString());
            return ResultMessage.createFail("金额有误!");
        }

        if (money <= 0) {
            return ResultMessage.createFail("金额有误!");
        }


        Long total_fee = MathUtil.transForMoney(MathUtil.transScaleMoney(MathUtil.transForMoney(money)));


        Long act_fee = total_fee;
        if (StringUtils.isNotBlank(act_total)) {
            Double act_money = null;
            try {
                act_money = Double.parseDouble(act_total);
            } catch (NumberFormatException e) {
                log.info("微信扫码支付金额有误!!!  {}", e.toString());
                return ResultMessage.createFail("金额有误!");
            }

            if (act_money <= 0) {
                return ResultMessage.createFail("金额有误!");
            }

            act_fee = MathUtil.transForMoney(MathUtil.transScaleMoney(MathUtil.transForMoney(act_money)));
        }

        if (act_fee > total_fee) {
            return ResultMessage.createFail("折扣后金额不能大于原来金额");
        }


        Map<String, Object> parmdata = new HashMap<>();

        parmdata.put("total_fee", total_fee);
        parmdata.put("act_fee", act_fee);
        parmdata.put("device_id", deviceId);

        if (StringUtils.isNotBlank(cpnId)) {
            parmdata.put("cardDetailId", cpnId);
        }

        String s = null;
        ResultMessage rest = null;
        try {
            String jsondata = JSON.toJSONString(parmdata);
            log.info("支付宝jsapi支付推送到api>>>>>> 参数 {}", jsondata);
            s = HttpClientUtil.doPostJson(PayApidomain + "/alJsPay/api/payorder", jsondata);
            rest = JSON.parseObject(s, ResultMessage.class);
            if (!Integer.valueOf(1).equals(rest.getCode())) {
                log.error("<<<<<<支付宝jsapi支付获取api结果失败 deviceId : {}  message: {}", deviceId, rest.getMessage());
                return ResultMessage.createFail(rest.getMessage());
            }
            log.info("支付宝jsapi支付推送到api获取结果<<<<<< 参数 {}", rest.getData());
        } catch (Exception e) {
            log.warn("支付宝jsapi支付失败!!! {}", e.toString());
            return ResultMessage.createFail("支付失败!!!");
        }

        return ResultMessage.createSuccess(rest.getData());

    }


    /**
     * 准备支付宝支付页面
     *
     * @param deviceId
     * @param model
     */
    @Override
    public void getAliAuth(String deviceId, Model model) {
        TerminalInfo terminalInfo = this.terminalInfoDao.getByDeviceId(deviceId);
        if (terminalInfo == null) {
            //二维码deviceId不存在
            return;
        }
        StoreInfo storeInfo = this.storeInfoDao.getById(terminalInfo.getStoreId());


        model.addAttribute("deviceId", deviceId);
        model.addAttribute("storeName", storeInfo.getName());
    }


}
